<template>
  <div>
    <el-select v-model="unitId" clearable placeholder="请选择单位" filterable>
      <el-option v-for="unit in unitOptions"
                 :key="unit.id" :label="unit.name" :value="unit.id"/>
    </el-select>
  </div>
</template>

<script>
import { getUnitSimple } from "@/api/base/unit"
export default {
  name: "SelectUnit",
  props: {
    value: {
      required: true
    }    
  },
  data() {
    return {
      unitOptions: [],
      unitId: this.value,
    }
  },
  watch: {
    value(newVal) {
      this.unitId = newVal;
    },
    unitId(newVal, oldVal){
      this.$emit("input", this.unitId)
    }
  },
  created() {
    this.getUnitListFun();
  },
  methods: {
    /** 查询单位列表 */
    getUnitListFun(){
      getUnitSimple().then(response => {
        this.unitOptions = response.data;
      })
    },
  }
};
</script>

